package cn.haocxy.jfm.filelist;

import java.util.Comparator;
import java.util.HashMap;

import cn.haocxy.jfm.file.FileType;
import cn.haocxy.jfm.file.FilePath;
import cn.haocxy.jfm.gui.FileItemGUI;

public abstract class Sorter implements Comparator<FileItemGUI> {

	private static final HashMap<FileType, Integer> typeSort = new HashMap<>();
	
	static {
		s(FileType.FOLDER, 0);
		s(FileType.EXE, 1);
	}
	
	private static void s(FileType type, int sort) {
		typeSort.put(type, sort);
	}
	
	private static int getTypeSort(FileType type) {
		Integer sort = typeSort.get(type);
		if (sort == null) {
			return Integer.MAX_VALUE;
		} else {
			return sort;
		}
	}
	
	private static int compType(FileType t1, FileType t2) {
		return getTypeSort(t1) - getTypeSort(t2);
	}
	
	@Override
	public int compare(FileItemGUI a, FileItemGUI b) {
		
		FileItem item1 = a.getModel();
		FileItem item2 = b.getModel();
		
		FileType type1 = item1.getFileType();
		FileType type2 = item2.getFileType();
		
		int comp = compType(type1, type2);
		if (comp != 0) {
			return comp;
		}
		
		return comp(item1.toFilePath(), item2.toFilePath());
	}
	
	protected abstract int comp(FilePath item1, FilePath item2);
}
